package com.bua.net;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: shenran
 * Date: 13-7-1
 * Time: 上午11:18
 * To change this template use File | Settings | File Templates.
 */
public class RequestManager {
    private ArrayList<RequestTask> requestList=new ArrayList<RequestTask>();
    private ArrayList<RequestTask> active = new ArrayList<RequestTask>();
    private static RequestManager instance;
    public static RequestManager getInstance() {
        if (instance == null)
            instance = new RequestManager();
        return instance;
    }
    public  void appendRequest(RequestTask task)
    {
        requestList.add(task);
        if (active.size() < 5)
            startNext();

    }
    private void startNext() {
        if (!requestList.isEmpty()) {
            RequestTask next = requestList.get(0);
            requestList.remove(0);
            active.add(next);

            Thread thread = new Thread(next);
            thread.start();
        }
    }

    public void didComplete(RequestTask runnable) {
        active.remove(runnable);
        startNext();
    }
}
